//
//  smloginController.m
//  shimao
//
//  Created by amy zhang on 12-6-27.
//  Copyright (c) 2012年 yst. All rights reserved.
//

#import "smloginController.h"
#import "smSearchListController.h"
#import "smMboSmMboDB.h"


@interface smloginController () <UITextFieldDelegate,MBProgressHUDDelegate>

@property (nonatomic, strong) UITextField* userNameTextField;
@property (nonatomic, strong) UITextField* userPassWordTextField;

@end

@implementation smloginController

@synthesize userNameTextField = _userNameTextField;
@synthesize userPassWordTextField = _userPassWordTextField;



- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    [self.navigationController.navigationBar setHidden:YES];
    
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"login.jpg"]]];
    
    
    UIButton* backBt = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBt setFrame: self.view.bounds];
    [backBt setBackgroundColor:[UIColor clearColor]];
    //定义点击时的响应函数
	[backBt addTarget:self action:@selector(missKeyBoard) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:backBt];
    
    // Button 代码参照这里写
    UIButton* loginBt = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBt setFrame:CGRectMake(470, 575, 180, 50)];
    [loginBt setImage:[UIImage imageNamed:@"longinButton_up.png"] forState:UIControlStateNormal];
    [loginBt setImage:[UIImage imageNamed:@"longinButton_down.png"] forState:UIControlStateHighlighted];
    [loginBt setBackgroundColor:[UIColor clearColor]];
    
    
    //定义点击时的响应函数
	[loginBt addTarget:self action:@selector(loginbuttonPress) forControlEvents:UIControlEventTouchUpInside]; 
    
    [self.view addSubview:loginBt];
    
    self.userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(300, 430, 340, 50)];
    //[self.userNameTextField setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    self.userNameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  
    [self.userNameTextField setPlaceholder:@"点击输入用户名"];
    self.userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;//编辑时，可以右边按钮全删除内容
    
    //菊花
    [self.userNameTextField setText:@"shimao"];
    
    [self.userNameTextField setDelegate:self];
    
    //首字母小写
    [self.userNameTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];

    //[self.userNameTextField setBackgroundColor:[UIColor redColor]];
    [self.view addSubview: self.userNameTextField];
    
    self.userPassWordTextField = [[UITextField alloc] initWithFrame:CGRectMake(300, 500, 340, 50)];
    self.userPassWordTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  
    [self.userPassWordTextField setPlaceholder:@"点击输入密码"];
    self.userPassWordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;//编辑时，可以右边按钮全删除内容
    [self.userPassWordTextField setDelegate:self];
    [self.userPassWordTextField setSecureTextEntry:YES];
    //[self.userPassWordTextField setBackgroundColor:[UIColor redColor]];
    [self.view addSubview: self.userPassWordTextField];
    
    //菊花
    [self.userPassWordTextField setText:@"shimao"];
    
}


- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController.navigationBar setHidden:YES];
}

- (void)viewDidAppear:(BOOL)animated {
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController.navigationBar setHidden:NO];
}


- (void)viewDidDisappear:(BOOL)animated {
    
}

-(void) loginbuttonPress {
    
    if ([self.userNameTextField.text isEqualToString:@""] || self.userNameTextField.text == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"用户名为空" message:@"请输入用户名"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        
        //自动启动用户名编辑框
        [self.userNameTextField becomeFirstResponder]; 
    }
    else if ([self.userPassWordTextField.text isEqualToString:@""] || self.userPassWordTextField.text == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"密码为空" message:@"请输入密码"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        
        //自动启动密码编辑框
        [self.userPassWordTextField becomeFirstResponder]; 
    }
    else {
        //登陆///////网络没好，所以模拟
        [self showMbprogress];
        [self performSelector:@selector(LoginCallBack) withObject:nil afterDelay:2];//30秒后检查
    }
}

-(void) LoginCallBack {
    
    [self hideMbProgress];
    
    if ([self.userNameTextField.text isEqualToString: @"shimao"]==YES && [self.userPassWordTextField.text isEqualToString:@"shimao"]==YES) {
        
        //登陆成功
//        [self dismissModalViewControllerAnimated:YES];
        //模拟登陆成功，跳转订单管理
        [self.view setHidden:YES];
        //[self.navigationController popViewControllerAnimated:YES];
//        smSearchListController* orderSearchController = [[smSearchListController alloc] init];
//        
//        [orderSearchController setHidesBottomBarWhenPushed:NO];
//        [self.navigationController pushViewController:orderSearchController animated:YES];
        
       [smMboSmMboDB subscribe];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"密码错误" message:@"默认账号密码:shimao"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

-(void) missKeyBoard {
    
    [self.userNameTextField resignFirstResponder]; 
    [self.userPassWordTextField resignFirstResponder]; 
}


//iPad键盘的Retuan...电脑的回车键
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    if (textField == self.userNameTextField) {
        
        //用户名输入完成，点击键盘Return
        //自动弹出密码输入
        [self.userPassWordTextField becomeFirstResponder];
        
    }else if (textField == self.userPassWordTextField) {
        //启动登陆
        [self loginbuttonPress];
    }
    return YES;
}

@end

